package cn.gbase.jiangsu.data.transfer.action;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.nutz.dao.Cnd;
import org.nutz.dao.QueryResult;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.Scope;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import cn.gbase.jiangsu.data.transfer.bean.CompanyCount;
import cn.gbase.jiangsu.data.transfer.bean.CompanyUser;
import cn.gbase.jiangsu.data.transfer.bean.SupplierUser;
import cn.gbase.jiangsu.data.transfer.service.CompanyCountService;
import cn.gbase.jiangsu.data.transfer.util.PropertyFilter;
import cn.gbase.jiangsu.data.transfer.util.SystemContext;

@IocBean
@At("/caiwu")
public class CaiWuAction {

	@Inject
	private CompanyCountService companyCountService;

	@At("/countMoney")
	@Ok("jsp:/companyCw/count")
	public Object countMoney(HttpServletRequest request,
			@Attr(scope = Scope.SESSION, value = SystemContext.COMPANYUSER) CompanyUser companyUser) {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		Cnd cnd1 = PropertyFilter.toCnd(filters, "id", "DESC");
		Cnd cnd2 = PropertyFilter.toCnd(filters, "id", "DESC");
		Cnd cnd3 = PropertyFilter.toCnd(filters, "id", "DESC");
		//物业公司商品分成所有的收入
		BigDecimal spfcCount = companyCountService.wygsFwfCount(companyUser, cnd1);
		//物业公司服务的收入
		BigDecimal spCount = companyCountService.wygsSpCount(companyUser, cnd2);
		//物业公司的运费
		BigDecimal yfCount = companyCountService.ddSumCount(companyUser, cnd3);
		//得到物业公司的所有收入
		BigDecimal sumCount = spfcCount.add(spCount).add(yfCount);
		//物业公司盈利的钱数
		BigDecimal ylCount = sumCount.subtract(sumCount);

		CompanyCount companyCount = new CompanyCount();
		companyCount.setSumCount(sumCount);
		companyCount.setYlCount(ylCount);
		companyCount.setShCount(new BigDecimal(0));

		return companyCount;
	}

	@At("/countZsrMoney")
	@Ok("jsp:/companyCw/zsrCount")
	public void countZsrMoney(HttpServletRequest request,
			@Attr(scope = Scope.SESSION, value = SystemContext.COMPANYUSER) CompanyUser companyUser) {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		Cnd cnd1 = PropertyFilter.toCnd(filters, "id", "DESC");
		Cnd cnd2 = PropertyFilter.toCnd(filters, "id", "DESC");
		Cnd cnd3 = PropertyFilter.toCnd(filters, "id", "DESC");
		//物业公司商品分成所有的收入
		BigDecimal spfcCount = companyCountService.wygsFwfCount(companyUser, cnd1);
		//物业公司服务的收入
		BigDecimal spCount = companyCountService.wygsSpCount(companyUser, cnd2);
		//物业公司的运费
		BigDecimal yfCount = companyCountService.ddSumCount(companyUser, cnd3);

		request.setAttribute("spfcCount", spfcCount);
		request.setAttribute("spCount", spCount);
		request.setAttribute("yfCount", yfCount);
		request.setAttribute("createTimeBegin", request.getParameter("filter_GES_createTime"));
		request.setAttribute("createTimeEnd", request.getParameter("filter_GES_createTime"));
	}

	@At("/getSpsrList/*")
	@Ok("jsp:/companyCw/spsrList")
	public Object getSpsrList(String createTimeBegin, String createTimeEnd, @Param("p") int pageNum,
			@Param("numPerPage") int numPerPage,
			@Attr(scope = Scope.SESSION, value = SystemContext.COMPANYUSER) CompanyUser companyUser) {
		return companyCountService.getSpsrList(createTimeBegin, createTimeEnd, companyUser, pageNum, numPerPage);
	}
	
	/**
	 * 分页查询账目（账目管理页面）.
	 * 
	 * @param currentPage
	 * @param groupId
	 * @param addTime
	 * @param keyword
	 * @param req
	 * @throws UnsupportedEncodingException
	 */
	@At("/listAccount")
	@Ok("jsp:/companyCw/listCompanyAccounts")
	public QueryResult listSupplierAccounts(@Param("p") int currentPage,
			@Param("accountPeriod") String accountPeriod,
			@Param("supplierId") int supplierId, HttpServletRequest request, HttpSession session) {
		CompanyUser u = (CompanyUser) session.getAttribute("companyUser");
		
		// 回传表单输入
		Map<String, String> itemMap = new HashMap<String, String>();
		itemMap.put("accountPeriod", accountPeriod);
		itemMap.put("supplierId", "" + u.getCompanyId());
		request.setAttribute("formdata", itemMap);


		return companyCountService.getCompanyAccountsList(currentPage,
				accountPeriod, u.getCompanyId());
	}
}
